package com.jbm.jbmsupplier.zp.adapters;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.jbm.core.iface.IAcross;
import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.net.NetInterChat;
import com.jbm.jbmsupplier.view.DialogLoading;
import com.jbm.jbmsupplier.zp.entity.Buddy_ListData;
import com.jbm.jbmsupplier.zp.liaotian.Personal_InformationActivity;
import com.jbm.jbmsupplier.zp.sqldata.MySqliteUtlis;
import com.jbm.jbmsupplier.zp.utils.ClassUser;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.io.Serializable;
import java.util.ArrayList;

import static com.jbm.jbmsupplier.zp.utils.TableName.BUDDY;
import static com.jbm.jbmsupplier.zp.utils.TableName.CHAT;
import static com.jbm.jbmsupplier.zp.utils.TableName.RECENT;

/**
 * Created by donar_work on 2017/3/9.
 */

public class Buddy_ListAdapter extends RecyclerView.Adapter<Buddy_ListAdapter.ViewHolder> {
    ViewHolder holders;
    Context mContext;
    private LayoutInflater inflater;
    private ArrayList<Buddy_ListData> mData;
    private int local_id;

    private final int DELETEFRIEND=0001;
    private boolean IsCave;
    private PopupWindow popupWindow;
    private int t_IDS;
    ClassUser ClassUser;
    private Dialog builder;

    private AlertDialog bus;

    public Buddy_ListAdapter(Context mContext , ArrayList<Buddy_ListData> mData,int local_id){
        this.mContext = mContext;
        this.mData = mData;
        this.local_id = local_id;
        inflater = LayoutInflater.from(mContext);
        ClassUser=new ClassUser(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.buddy_list_text, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holders=holder;
      final  Buddy_ListData buddy_listData = mData.get(position);
        holder.t_ID.setText(buddy_listData.getNickname()+"");
        if (buddy_listData.getHead_portrait()==null){
            holder.nameimage.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.pic));
        }else if (buddy_listData.getHead_portrait().equals("")||buddy_listData.getHead_portrait().equals("null")){
            holder.nameimage.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.pic));
        }else {
            ImageOptions i=new ImageOptions.Builder().setCircular(true).setFailureDrawable(mContext.getResources().getDrawable(R.mipmap.pic)).build();
            x.image().bind(holder.nameimage,mData.get(position).getHead_portrait(),i);

        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                t_IDS = mData.get(position).getM_account_id();
                bundle.putSerializable("main", (Serializable) mData.get(position));
                intent.putExtras(bundle);
                intent.setClass(mContext,Personal_InformationActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
            }
        });


        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                setPopuDele(mData.get(position));
                return true;            }

        });

    }


    DialogLoading dialogLoading;
    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView t_ID;
       ImageView nameimage;

        public ViewHolder(View itemView) {
            super(itemView);
            t_ID = (TextView) itemView.findViewById(R.id.t_ID);
            nameimage=(ImageView) itemView.findViewById(R.id.nameimage);
        }
    }
public void setPopuDele(final Buddy_ListData buddy_ListData ) {
    t_IDS = buddy_ListData.getM_account_id();
    popupWindow = new PopupWindow();
    View view = View.inflate(mContext, R.layout.popuitem, null);
    popupWindow.setContentView(view);
    Button bottde = (Button) view.findViewById(R.id.deletefriend);
    popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
    // 使其聚集  
    popupWindow.setFocusable(true);
    // 设置允许在外点击消失  
    popupWindow.setOutsideTouchable(false);
    // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景  
    popupWindow.setBackgroundDrawable(new BitmapDrawable());
//    popupWindow.setAnimationStyle(R.style.popuwindow);
//    WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    // 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半  
//    int xPos = windowManager.getDefaultDisplay().getWidth() / 2 - popupWindow.getWidth()/2;
//    popupWindow.showAsDropDown(holders.itemView, xPos,-160);
    popupWindow.showAtLocation(holders.itemView,Gravity.CENTER,0,0);
    bottde.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    NetInterChat netinterchat = new NetInterChat(handler);
                    netinterchat.RomeFriend(DELETEFRIEND, ClassUser.getLogin(), buddy_ListData.getM_account_id());
                }
            }).start();

        }
    });
}


Handler handler=new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case DELETEFRIEND:
                IAcross drow=(IAcross)msg.obj;
                if (drow.m_code==200){
                   if (popupWindow!=null){
                       popupWindow.dismiss();
                   }

                  MySqliteUtlis.GetInstan(mContext).deletedata(RECENT ,String.valueOf(t_IDS) ,ClassUser.getLogin());
                 MySqliteUtlis.GetInstan(mContext).deleteFriendsdata(BUDDY , CHAT, String.valueOf(t_IDS),ClassUser.getLogin());
                    Intent intent = new Intent();
                    intent.putExtra("messageFlag",50);
                    intent.setAction("DropContact");
                    mContext.sendBroadcast(intent);
                    Toast.makeText(mContext, "删除好友成功", Toast.LENGTH_SHORT).show();
                }else{
                    Log.e("main",drow.m_info+"删除好友失败");
                }

                break;
        }
    }
};

}
